home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / lib / can_access.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-13  |  3.2 KB  |  130 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: can_access.c,v 5.6 1993/05/14 03:52:10 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.6 $   $State: Exp $
  6.  *
  7.  *            Copyright (c) 1988-1992 USENET Community Trust
  8.  *            Copyright (c) 1986,1987 Dave Taylor
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log: can_access.c,v $
  17.  * Revision 5.6  1993/05/14  03:52:10  syd
  18.  * When compiled on a POSIX host PL22 failed checking whether the file is
  19.  * readable and a regular file or not. There was one `!' missing in the
  20.  * `if (S_ISREG(mode))' test which should read `if (! S_ISREG(mode))'.
  21.  * From: Jukka Ukkonen <ukkonen@csc.fi>
  22.  *
  23.  * Revision 5.5  1993/05/08  19:16:41  syd
  24.  * Remove symlink code from can_access, we dont care if its a symlink
  25.  * only if we can access the file pointed to by the symlink anyway and
  26.  * stat resolves to that file.
  27.  *
  28.  * Revision 5.4  1993/04/12  04:08:36  syd
  29.  * Fix if alignment
  30.  *
  31.  * Revision 5.3  1993/04/12  03:33:39  syd
  32.  * the posix macros to interpret the result of the stat-call.
  33.  * From: vogt@isa.de (Gerald Vogt)
  34.  *
  35.  * Revision 5.2  1992/12/12  01:29:26  syd
  36.  * Fix double inclusion of sys/types.h
  37.  * From: Tom Moore <tmoore@wnas.DaytonOH.NCR.COM>
  38.  *
  39.  * Revision 5.1  1992/10/03  22:41:36  syd
  40.  * Initial checkin as of 2.4 Release at PL0
  41.  *
  42.  *
  43.  ******************************************************************************/
  44.  
  45. /** can_access - can this user access this file using their normal uid/gid
  46.  
  47. **/
  48.  
  49. #include "headers.h"
  50. #include <sys/stat.h>
  51. #include <ctype.h>
  52. #include <errno.h>
  53.  
  54. #ifdef BSD
  55. # include <sys/wait.h>
  56. #endif
  57.  
  58. extern int errno;        /* system error number */
  59.  
  60. int
  61. can_access(file, mode)
  62. char *file; 
  63. int   mode;
  64. {
  65.     /** returns ZERO iff user can access file or "errno" otherwise **/
  66.  
  67.     int the_stat = 0, pid, w; 
  68.     struct stat stat_buf;
  69.     void _exit();
  70. #if defined(BSD) && !defined(WEXITSTATUS)
  71.     union wait status;
  72. #else
  73.     int status;
  74. #endif
  75.     register SIGHAND_TYPE (*istat)(), (*qstat)();
  76.     
  77. #ifdef VFORK
  78.     if ((pid = vfork()) == 0) {
  79. #else
  80.     if ((pid = fork()) == 0) {
  81. #endif
  82.       setgid(groupid);
  83.       setuid(userid);        /** back to normal userid **/
  84.  
  85.       errno = 0;
  86.  
  87.       if (access(file, mode) == 0) 
  88.         _exit(0);
  89.       else 
  90.         _exit(errno != 0? errno : 1);    /* never return zero! */
  91.       _exit(127);
  92.     }
  93.  
  94.     istat = signal(SIGINT, SIG_IGN);
  95.     qstat = signal(SIGQUIT, SIG_IGN);
  96.  
  97.     while ((w = wait(&status)) != pid && w != -1)
  98.         ;
  99.  
  100. #if    defined(WEXITSTATUS)
  101.     /* Use POSIX macro if defined */
  102.     the_stat = WEXITSTATUS(status);
  103. #else
  104. #ifdef BSD
  105.     the_stat = status.w_retcode;
  106. #else
  107.     the_stat = status >> 8;
  108. #endif
  109. #endif    /*WEXITSTATUS*/
  110.  
  111.     signal(SIGINT, istat);
  112.     signal(SIGQUIT, qstat);
  113.     if (the_stat == 0) {
  114.       if (stat(file, &stat_buf) == 0) {
  115. #ifndef _POSIX_SOURCE
  116.         w = stat_buf.st_mode & S_IFMT;
  117.         if (w != S_IFREG)
  118.           the_stat = 1;
  119. #else /* _POSIX_SOURCE */
  120.         w = stat_buf.st_mode;
  121.  
  122.         if (! S_ISREG(w))
  123.           the_stat = 1;
  124. #endif
  125.       }
  126.     }
  127.  
  128.     return(the_stat);
  129. }
  130.